Skip to content

feat(prefer-use-template-ref): add support for fix option #2632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Thomasan1999
Copy link
Contributor

@Thomasan1999 Thomasan1999 commented Nov 30, 2024

Addresses #2554 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add an test case using TypeScript?

<template>
  <div ref="root" />
</template>
<script setup lang="ts">
  import { ref } from 'vue';
  const root = ref<HTMLDivElement>();
</script>

should get autofixed to

<template>
  <div ref="root" />
</template>
<script setup lang="ts">
  import { ref,useTemplateRef } from 'vue';
  const root = useTemplateRef<HTMLDivElement>('root');
</script>

Note that you'll need to specify the parser in the test:

languageOptions: {
  parserOptions: {
    parser: require.resolve('@typescript-eslint/parser')
  }
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late response, I added the test but I am not sure about keeping the generic type parameter. I see as one of the main benefits of useTemplateRef function that you don't have to use the type parameters because the Vue Language Server would create it for you. With ref you did not have other option but to manually define it. Wouldn't it be better for the users to automatically get rid of it?

const lastImportSpecifier = importSpecifiers[importSpecifiers.length - 1]

// @ts-ignore
return fixer.insertTextAfter(lastImportSpecifier, `,useTemplateRef`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fixer.insertTextAfter(lastImportSpecifier, `,useTemplateRef`)
return fixer.insertTextAfter(lastImportSpecifier, `, useTemplateRef`)

Comment on lines 65 to 67
const body = sourceCode.ast.body

const imports = body.filter((node) => node.type === 'ImportDeclaration')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const body = sourceCode.ast.body
const imports = body.filter((node) => node.type === 'ImportDeclaration')
const body = sourceCode.ast.body
const imports = body.filter((node) => node.type === 'ImportDeclaration')

Comment on lines 85 to 87
const lastImport = imports[imports.length - 1]

const importStatement = "import {useTemplateRef} from 'vue';"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Let's put the importStatement variable first because it's used in both following branches.
  2. Remove the empty line to make it a little more concise.
  3. Add spaces between the braces in the added code, to conform with the common Prettier style. (If users don't like it, they can and have to change it either way.)
Suggested change
const lastImport = imports[imports.length - 1]
const importStatement = "import {useTemplateRef} from 'vue';"
const importStatement = "import { useTemplateRef } from 'vue';"
const lastImport = imports[imports.length - 1]

Comment on lines 170 to 193
/** @type {Fix[]} */
const fixes = []

const replaceFunctionFix = fixer.replaceText(
scriptRef.node,
`useTemplateRef('${scriptRef.ref}')`
)

fixes.push(replaceFunctionFix)

if (!missingImportChecked) {
missingImportChecked = true

const missingImportFix = addUseTemplateRefImport(
context,
fixer
)

if (missingImportFix) {
fixes.push(missingImportFix)
}
}

return fixes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a little easier to understand (and shorter) without the fixes array:

Suggested change
/** @type {Fix[]} */
const fixes = []
const replaceFunctionFix = fixer.replaceText(
scriptRef.node,
`useTemplateRef('${scriptRef.ref}')`
)
fixes.push(replaceFunctionFix)
if (!missingImportChecked) {
missingImportChecked = true
const missingImportFix = addUseTemplateRefImport(
context,
fixer
)
if (missingImportFix) {
fixes.push(missingImportFix)
}
}
return fixes
const replaceFunctionFix = fixer.replaceText(
scriptRef.node,
`useTemplateRef('${scriptRef.ref}')`
)
if (!missingImportChecked) {
missingImportChecked = true
const missingImportFix = addUseTemplateRefImport(
context,
fixer
)
if (missingImportFix) {
return [replaceFunctionFix, missingImportFix]
}
}
return replaceFunctionFix

# Conflicts:
#	docs/rules/index.md
#	tests/lib/rules/prefer-use-template-ref.js
Copy link

changeset-bot bot commented Jun 7, 2025

⚠️ No Changeset found

Latest commit: f6aefde

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants